/**
 * 
 */
package com.gmail.sabuj.career.qna.core.fsm;

import java.util.concurrent.atomic.AtomicInteger;


/**
 * @author Sabuj Das | sabuj.das@gmail.com
 *
 */
public class State<V> {
	
	public static final String STATE_NAME_PREFIX = "S_";
	
	private volatile static AtomicInteger count = new AtomicInteger(0);
	
	private final String name;
	
	private V value;
	private State<V> before; 
	private State<V> after;
	
	public State() {
		name = STATE_NAME_PREFIX + count.incrementAndGet();
	}
	
	public State(String name) {
		this.name = name;
	}

	public V getValue() {
		return value;
	}

	public void setValue(V value) {
		this.value = value;
	}

	public State<V> getBefore() {
		return before;
	}

	public void setBefore(State<V> before) {
		this.before = before;
	}

	public State<V> getAfter() {
		return after;
	}

	public void setAfter(State<V> after) {
		this.after = after;
	}

	public String getName() {
		return name;
	}
	
	@Override
	protected void finalize() throws Throwable {
		count.decrementAndGet();
		super.finalize();
	}
	
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("State [name=");
		builder.append(name);
		builder.append("]");
		return builder.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof State)) {
			return false;
		}
		State other = (State) obj;
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		} else if (!name.equals(other.name)) {
			return false;
		}
		return true;
	}

}
